﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ymatou.ServiceCenter.Common
{
    public class AppSettings : IConfigSettings
    {
        public bool Exists(string key)
        {
            string value = ConfigurationManager.AppSettings.Get(key);
            return value != null;
        }

        public string GetString(string key)
        {
            string value = ConfigurationManager.AppSettings.Get(key);
            return value;
        }

        public IList<TKey> GetList<TKey>(string key) where TKey : IConvertible
        {
            string value = ConfigurationManager.AppSettings.Get(key);
            if (string.IsNullOrEmpty(value))
                return null;
            string[] array = value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            IList<TKey> list = new List<TKey>();
            foreach (string str in array)
            {
                TKey tKey = (TKey)Convert.ChangeType(str, typeof(TKey));
                list.Add(tKey);
            }
            return list;
        }

        public IDictionary<TKey, TValue> GetDictionary<TKey, TValue>(string key)
            where TKey : IConvertible
            where TValue : IConvertible
        {
            string value = ConfigurationManager.AppSettings.Get(key);
            if (string.IsNullOrEmpty(value))
                return null;

            if (!value.Contains(":"))
                throw new Exception("格式错误，正确格式形如：Key1:Value1;Key2:Value2");
            IDictionary<TKey, TValue> dic = new Dictionary<TKey, TValue>();
            string[] items = value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string item in items)
            {
                if (!value.Contains(":"))
                    continue;
                string[] keyValuePairs = item.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                if (keyValuePairs.Length != 2) continue;
                string strKey = keyValuePairs[0];
                string strValue = keyValuePairs[1];
                TKey tKey = (TKey)Convert.ChangeType(strKey, typeof(TKey));
                TValue tValue = (TValue)Convert.ChangeType(strValue, typeof(TValue));
                dic.Add(new KeyValuePair<TKey, TValue>(tKey, tValue));
            }
            return dic;
        }

        public T Get<T>(string key, T defaultValue = default(T))
        {
            string value = ConfigurationManager.AppSettings.Get(key);
            if (string.IsNullOrEmpty(value))
                return defaultValue;
            else
                value = value.Trim();
            Type conversionType = typeof(T);
            if (conversionType.IsEnum)
            {
                int i;
                bool flag = int.TryParse(value, out i);
                if (flag)
                    return (T)Enum.Parse(conversionType, value, true);
                else
                    return (T)Enum.ToObject(conversionType, i);
            }

            if (conversionType == typeof(Guid))
            {
                return (T)(object)new Guid(value);
            }

            if (conversionType == typeof(Type))
            {
                return (T)(object)Type.GetType(value, true);
            }

            if (conversionType.GetInterface("IConvertible") != null)
            {
                return (T)Convert.ChangeType(value, conversionType);
            }
            return default(T);
        }
    }
}
